import {browsesListApi, queryDateApi, delApi} from './api';
import { Toast } from 'antd-mobile';
import { Route } from 'dva/router';


export default {
    namespace: 'footPrint',
    state: {
        browsesList: {},
        curDateList: [],

        selectDate: '',
    },
    reducers:{
        save(state, { data }) {
            return { ...state, ...data };
        },

        saveDate (state, { data }) {
            var curDateList = new Set(data.map(v => v.time));
            console.log(data, curDateList, "saveDate");
            return { ...state, curDateList};
        },
        dateSave (state, { data }) {
            console.log(data, "data......................");
            return { ...state, ...{selectDate: data}};
        },
    },
    effects: {
        *browsesListEFF({obj }, { put, call, select }) {
            Toast.loading('加载中..',3);
            const selectDate = yield select(state => state.footPrint.selectDate);
            const list = yield select(state => state.footPrint.browsesList.list);

            const result = yield call(!selectDate ? browsesListApi : queryDateApi, {...obj, date: selectDate});
            if(result.code == 200){
                if (result.ret.pageNo == 1) {
                    yield put({type: 'save', data: {browsesList: result.ret || {}}});
                    yield put({type: 'saveDate', data: result.ret.list});
                } else {
                    yield put({type: 'save', data: {browsesList: {...result.ret, list: [...list, ...result.ret.list]} }});
                    yield put({type: 'saveDate', data: [...list, ...result.ret.list]});
                }
                Toast.hide();
            }else{

            }
        },

        *queryDateEFF({obj }, { put, call }) {
            console.log(obj, "date");
            Toast.loading('加载中..',3);
            const result = yield  call(queryDateApi, obj);
            if(result.code == 200){
                yield put({type:'save',data:{browsesList:result.ret || []}});
                yield put({type: 'saveDate', data: result.ret.list});
                Toast.hide();
            }else{

            }
        },

        *delEFF({obj }, { put, call }) {
            console.log(obj, "date");
            Toast.loading('加载中..',3);
            const result = yield call(delApi, obj);
            if(result.code == 200){
                yield put({type:'save', data: ''});
                Toast.hide();
            }else{

            }
            return result;
        },
    },
    subscriptions: {

    },
};
